package com.liujit.bms.pc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.pc.domain.model.BlogArticle;
import com.liujit.bms.pc.domain.query.BlogArticleQuery;
import com.liujit.bms.pc.domain.result.BlogArticleResult;
import com.liujit.bms.pc.mapper.BlogArticleMapper;
import com.liujit.bms.pc.service.BlogArticleService;
import com.liujit.bms.pc.service.BlogCategoryService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 博客文章Service实现类
 * @Author liujun
 * @Date 2021-07-03 14:55:02
 */
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {

    private BlogCategoryService blogCategoryService;

    @Autowired
    public void setBlogCategoryService(BlogCategoryService blogCategoryService) {
        this.blogCategoryService = blogCategoryService;
    }

    @Override
    public PageBean<BlogArticleResult> getPage(BlogArticleQuery query) {
        if (StrUtil.isBlank(query.getSortName())) {
            query.setSortName(ColumnConstant.IS_TOP);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<BlogArticle> page = Pagination.page(query);
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogArticle::getIsOpen, GlobalConstant.OPEN_YES);
        if (CollectionUtil.isNotEmpty(query.getIds())) {
            wrapper.in(BlogArticle::getId, query.getIds());
        }
        if (query.getCategoryId() != null) {
            wrapper.eq(BlogArticle::getCategoryId, query.getCategoryId());
        }
        if (StrUtil.isNotBlank(query.getTitle())) {
            wrapper.like(BlogArticle::getTitle, query.getTitle());
        }
        IPage<BlogArticle> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogArticleResult.class);
    }

    @Override
    public Map<String, Object> getUp(Date updateTime) {
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BlogArticle::getId, BlogArticle::getTitle, BlogArticle::getUpdateTime);
        wrapper.orderByDesc(BlogArticle::getUpdateTime);
        wrapper.lt(BlogArticle::getUpdateTime, updateTime);
        List<Map<String, Object>> list = super.listMaps(wrapper);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Map<String, Object> getDown(Date updateTime) {
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BlogArticle::getId, BlogArticle::getTitle, BlogArticle::getUpdateTime);
        wrapper.orderByAsc(BlogArticle::getUpdateTime);
        wrapper.gt(BlogArticle::getUpdateTime, updateTime);
        List<Map<String, Object>> list = super.listMaps(wrapper);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BlogArticleResult get(Long id) {
        BlogArticle blogArticle = super.getById(id);
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        BlogArticleResult result = DozerConvertor.convertor(blogArticle, BlogArticleResult.class);
        // 分类名称
        result.setCategoryName(blogCategoryService.getNameById(result.getCategoryId()));
        return result;
    }
}
